<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增广告')"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-advertising-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">广告标题：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="title" id="title">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">广告内容：</label>
            <div class="col-sm-8">
                <textarea type="text" name="content" class="form-control" th:rows="5" placeholder="请输广告内容"></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">图片：</label>
            <div class="col-sm-3">
                <div id="thumbnails"></div>
                <input class="form-control" id="imageUrl" type="hidden" name="imageUrl"/>
            </div>
            <div class="col-sm-5">
                <a class="btn btn-success" onclick="BrowseServer('Images:/', 'imageUrl');">
                    <i class="fa fa-plus"></i>选择
                </a>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">图片名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="name" id="name" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">图片跳转链接地址：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="jumpUrl" id="jumpUrl">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">排序：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="sort" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否显示：</label>
            <div class="col-sm-9">
                <div class="radio-box" th:each="dict : ${@dict.getType('cms_show_hide')}">
                    <input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}"
                           th:checked="${dict.default}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea type="text" name="remark" class="form-control" th:rows="5" placeholder="请输入文本"></textarea>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: CKFinder-js"/>
<script type="text/javascript" th:inline="javascript">
    var prefix = ctx + "cms/advertising";
    $(function () {

    })

    function BrowseServer(startupPath, functionData) {
        //使用“CKFinder”类在页面中呈现CKFinder
        var finder = new CKFinder();
        //CKFinder的安装路径 (default = "/ckfinder/").
        finder.basePath = ctx + 'ckfinder/ckfinder.html?Type=Images';
        //打开的路径: "Type:/path/to/directory/"
        finder.startupPath = startupPath;
        //要更新的字段的Id
        finder.selectActionData = functionData;
        //选择文件时调用的函数的名称
        finder.selectActionFunction = SetFileField;
        //选择缩略图时调用的函数的名称
        finder.selectThumbnailActionFunction = ShowThumbnails;
        // Launch CKFinder
        finder.popup({width: 777, height: 557, popupTitle: 'CKFinder Popup', popupFeatures: 'menubar=yes,modal=yes'});
    }

    //选择文件时调用
    function SetFileField(fileUrl, data) {
        //自定义缩略图
        var sFileName = this.getSelectedFile().name;
        document.getElementById('thumbnails').innerHTML = '';
        document.getElementById('thumbnails').innerHTML +=
            '<div>' +
            '<img src="' + fileUrl + '" style="max-width:120px;max-height:100px"/>' +
            '<a href="javascript:" onclick="clearImge(this);">×</a>' +
            '<div>' +
            '<a href="' + data["fileUrl"] + '" target="_blank">' + sFileName + '</a>' +
            '</div>' +
            '</div>';
        //更新表单imageUrl的值
        $("#imageUrl").val(data["fileUrl"]);
        //自动填充图片名称
        $("#name").val(sFileName);
    }

    //选择一个缩略图时将调用该函数
    function ShowThumbnails(fileUrl, data) {
        var sFileName = this.getSelectedFile().name;
        //每次选择清空原来的值
        document.getElementById('thumbnails').innerHTML = '';
        //重新添加缩略图
        document.getElementById('thumbnails').innerHTML +=
            '<div class="thumb">' +
            '<img src="' + fileUrl + '" style="max-width:120px;max-height:100px"/><a href="javascript:" onclick="clearImge(this);">×</a>' +
            '<div style="padding-left: 25%">' +
            '<a href="' + data["fileUrl"] + '" target="_blank">' + sFileName + '</a>' +
            '</div>' +
            '</div>';
        //更新表单imageUrl的值
        $("#imageUrl").val(data["fileUrl"]);
        //自动填充图片名称
        $("#name").val(sFileName);

        //它不需要返回任何值
        //当返回false时，CKFinder将不会自动关闭。
        return true;
    }

    //清除缩略图、图片路径
    function clearImge(eml) {
        document.getElementById('thumbnails').innerHTML = '';
        $("#imageUrl").val("");
        $("#name").val("");
    }

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/addAdvertising", $('#form-advertising-add').serialize());
        }
    }

    function doSubmit(index, layero) {
        var body = $.modal.getChildFrame(index);
        $.modal.close(index);
    }
</script>
</body>
</html>
